package com.iamk.semanticsegment;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

import weka.classifiers.functions.SMO;
import weka.core.Instances;

import com.iamk.util.GetInstances;
import com.iamk.util.ImageUtil;


public class SemanticSegment {
	private int[][] mask;
	private BufferedImage imgOriginal;
	private BufferedImage imgSegment;
	FuzzyCMean fuzzyCMean;
	private ColorFeature colorFeature;
	final static int M=2;
	final static int C=5;
	
	public SemanticSegment(BufferedImage imgOriginal) {
		this.setImgOriginal(imgOriginal);
		colorFeature=new ColorFeature(imgOriginal);
	}

	/**
	 * Method implement Semantic Segmentation 
	 */
	public void run(){
		// Get Pixel Features
		// Training Sample Selection using FCM
		// SVM Training
		// SVM Pixel Classification
		
		// Create features level-pixel

		// Run FCM out put hash train list test
		colorFeature.run();
		fuzzyCMean = new FuzzyCMean(colorFeature.colorFeatures, M, C,imgOriginal.getHeight(),imgOriginal.getWidth());
		fuzzyCMean.run();
		mask=fuzzyCMean.getMask();
		printMatrix(mask);
		// Run SVM
		SMO smo = new SMO();
		try {
			// get attribute, class
			int attr=fuzzyCMean.getTest().get(0).getFeatures().length;
			int clas=0;
			for(int i=0;i<fuzzyCMean.getTrain().size();i++){
				System.out.println("Size "+((ArrayList<Pixel>)fuzzyCMean.getTrain().get(""+(i+1))).size());
				if(((ArrayList<Pixel>)fuzzyCMean.getTrain().get(""+(i+1))).size()>0){
					clas++;
				}
			}
			smo.buildClassifier(GetInstances.getInstancesToHash(fuzzyCMean.getTrain(), attr, clas));
			// smo.classifyInstance();
			ArrayList<Pixel> test = fuzzyCMean.getTest();
			/** Doan nay theo t thi minh nen tao ra tap Test truoc,
			 * 	( Ham getInstances cua m, qua moi lan lap tao ra mot Instances moi chi co mot quan sat)  
			 *  Roi sau do lap qua tung quan sat cua no de tien hanh phan lop bang smo
			 *  Voi ca xem lai tham so cua phan lop SMO nhe
			 * */
			
			for (Pixel p : test) {
				Instances mInstancesTest = GetInstances.getInstances(p.features);
				p.label = (int) smo.classifyInstance(mInstancesTest.get(0));
				mask[p.x][p.y] = p.label;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public int[][] getMask() {
		return mask;
	}

	public BufferedImage getImgOriginal() {
		return imgOriginal;
	}

	public void setImgOriginal(BufferedImage imgOriginal) {
		this.imgOriginal = imgOriginal;
	}

	public BufferedImage getImgSegment() {
		return imgSegment;
	}
	public static void main(String[] args) {
		BufferedImage img = ImageUtil.readImage("C:\\Users\\Ken\\Desktop\\data-msrc\\Images\\1_18_s.bmp");
		SemanticSegment seg=new SemanticSegment(img);
		seg.run();
		System.out.println("H "+seg.mask.length +" W "+seg.mask[0].length );
		int[][] mask=seg.mask;
		for(int i=0;i<mask.length;i++){
			for(int j=0;j<mask[0].length;j++){
				System.out.print(mask[i][j]);
			}
			System.out.println();
		}
	}
	private void printMatrix(int[][] mask){
		for(int i=0;i<mask.length;i++){
			for(int j=0;j<mask[0].length;j++){
				System.out.print(mask[i][j]);
			}
			System.out.println();
		}
	}
}

